/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.funda.xmppserver.handlers;

import com.funda.xmppserver.ComponentParser;
import com.funda.xmppserver.XMPPClient;
import com.funda.xmppserver.mysql.DBManager;
import com.funda.xmppserver.presence.PresenceManager;
import com.funda.xmppserver.presence.Show;
import com.funda.xmppserver.writer.XMPPPacketWriter;
import com.funda.xmppserver.x.XDelay;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author kurinchi
 */
public class PresenceHandler implements ComponentParser {

    XMPPPacketWriter packetwriter;
    XMPPClient client;
    PresenceManager presencemanager;
    Connection conn;
    Statement smt;
    ResultSet rs;
    Boolean valid = true;
    String id;
    String type;
    int show = Show.AVAILABLE;
    String status = "";
    int priority = 10;
    ArrayList<Object> showlist;
    Map<Object, Object> delegates = new HashMap<Object, Object>();

    public PresenceHandler(XMPPPacketWriter packetwriter, XMPPClient client) {
        this.packetwriter = packetwriter;
        this.client = client;
        this.presencemanager = client.getPresencemanager();
        showlist=presencemanager.getShowlist();
        
    }

    public void parseElement(XMLStreamReader xmlReader) throws XMLStreamException {
        System.out.println("presence");
        show=Show.AVAILABLE;

        String type="";
        String to="";
        //checking for subscrption request
        for(int i=0;i<xmlReader.getAttributeCount();i++)
        {
            if(xmlReader.getAttributeLocalName(i).equalsIgnoreCase("type"))
            {
                type=xmlReader.getAttributeValue(i);
            }
            if(xmlReader.getAttributeLocalName(i).equalsIgnoreCase("to"))
            {
                to=xmlReader.getAttributeValue(i);
            }
        }

        //send subscription request
        if(type.equalsIgnoreCase("subscribe"))
        {
            System.out.println("Subscription Request...");
            packetwriter.sendSubscriptionrequestPresence(to);
            return;
        }
        
        //request accepted
        if(type.equalsIgnoreCase("subscribed"))
        {
            System.out.println("Subscribed....");

            client.getRoster().getRosteritems().put(to, "both");

            packetwriter.sendSubscriptionApprovedResponse(to);
            return;
        }

        //request rejected
        if(type.equalsIgnoreCase("unsubscribed"))
        {
            System.out.println("Unsubscribed...");

            client.getRoster().getRosteritems().remove(to);

            DBManager.updateRoster(client.getUsername(), "none", to.substring(0, to.indexOf("@")));

            return;
        }


        while (xmlReader.hasNext()) {
            xmlReader.next();
            int event = xmlReader.getEventType();

            if (event == XMLEvent.START_ELEMENT) {
                if (xmlReader.getLocalName().equalsIgnoreCase("show")) {
                    String sh = xmlReader.getElementText();
                    if (showlist.contains(sh)) {
                        show = showlist.indexOf(sh);
                    }

                }
                if (xmlReader.getLocalName().equalsIgnoreCase("status")) {
                    status = xmlReader.getElementText();

                }
                if (xmlReader.getLocalName().equalsIgnoreCase("priority")) {
                    priority = Integer.parseInt(xmlReader.getElementText());
                }
            }
            if (event == XMLEvent.END_ELEMENT) {
                if (xmlReader.getLocalName().equalsIgnoreCase("presence")) {
                    break;
                }
            }
        }
        System.out.println("Show : " + show + " Status : " + status + " Priority : " + priority);
        presencemanager.setShow(show);
        presencemanager.setStatus(status);
        presencemanager.setpriority(priority);

        packetwriter.updateAllRoster(presencemanager, presencemanager.getShowlist(),"");

        XDelay xdelay=new XDelay(client);
        xdelay.checkXDelaymsg();

    }
}
